from django.shortcuts import render
from rest_framework.views import APIView
from django.http import HttpResponse
from meiduo_mall.libs.captcha.captcha import captcha
from django_redis import get_redis_connection
from rest_framework.generics import GenericAPIView
import random
from rest_framework.response import Response
from celery_tasks.sms.tasks import send_sms_code
from . import serializers
import logging

logger = logging.getLogger('django')

from . import constants


# Create your views here.
# url('^sms_codes/(?P<mobile>1[3-9]\d{9})/$', views.SMSCodeView.as_view()),  # 短信验证码
class SMSCodeView(GenericAPIView):
    serializer_class = serializers.ImageCodeCheckSerializer

    def get(self, request, mobile):
        # 创建序列化器对象
        serializer = self.get_serializer(data=request.query_params)
        # 开启校验
        serializer.is_valid(raise_exception=True)

        # 生成短信验证码
        sms_code = '%06d' % random.randint(0, 999999)
        #不去发送短信了， 手动打印验证码
        logger.info(sms_code)
        # print(sms_code)

        # 存储短信验证码
        redis_conn = get_redis_connection('verify_codes')
        # redis_conn.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
        #
        # #发送短信频率
        # redis_conn.setex('send_flag_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)

        pl = redis_conn.pipeline()
        pl.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)

        # 发送短信频率
        pl.setex('send_flag_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)
        pl.execute()

        # 调用异步任务 发送短信

        send_sms_code.delay(mobile, sms_code)

        # 返回结果
        return Response({'message': 'OK'})


# url('^image_codes/(?P<image_code_id>[\w-]+)/$', views.ImageCodeView.as_view()),
class ImageCodeView(APIView):
    """提供图片验证码"""

    def get(self, request, image_code_id):
        # 生成图片验证码:text是验证码内容，需要保存到Redis；image是图片验证码数据，需要响应给前端
        text, image = captcha.generate_captcha()

        # 将图片验证码内容存储到redis数据库
        redis_conn = get_redis_connection('verify_codes')
        # redis_conn.setex('key', 'expires', 'value')
        redis_conn.setex('img_%s' % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES, text)

        # 将图片验证码图片数据响应到前端
        return HttpResponse(image, content_type='image/jpg')
